﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Classic.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Index</h2>
    <fieldset class="form">
        <h3>
            Wyszukiwanie pracowników przychodni:
        </h3>
        <div>
            <label for="worker_search_value">
                Kryterium:
            </label>            
            <input type="text" id="worker_search_value" />            
            <select id="worker_search_field">
                <option value="Worker.Surname">Nazwisko</option>
                <option value="Login">Login</option>                
            </select>
            <input type="checkbox" id="worker_activity" checked/>Szukaj aktywnych
            <input type="button" id="worker_search_button" value="Szukaj" onclick="javascript:GetWorkerSearchResult();" />
        </div>
        <div>
            <table class="grid" id="worker_search_result">
            </table>
        </div>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="HeadContent" runat="server">
    <script type="text/javascript" src="../../Scripts/jquery-1.4.1.js"></script>
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="ScriptContent" runat="server">
    <script type="text/javascript">

        function UpdateWorkerResultTableAnimated(data) {
            var resultTable = $("table#worker_search_result");
            resultTable.fadeOut("slow", function () {
                UpdateWorkerResultTable(data);
                resultTable.fadeIn("slow");
            });
        }

        function UpdateWorkerResultTable(data) {
            var resultTable = $("table#worker_search_result");
            resultTable.empty();
            resultTable.append($("<thead>").html("<tr><th>Imię i nazwisko</th><th>Login</th><th>Czy aktywny</th><th>Akcja</th></tr>"));
            var result = $("<tbody>");
            var hasElements = false;
            $.each(data, function (index, wu) {
                hasElements = true;
                var line = $("<tr>").attr("class", index % 2 == 0 ? "gridrow" : "gridrow_alternate");
                var element = $("<td>").attr("class", "center");

                var workerDetailAddr = "/WorkerUser/Detail/" + wu.Id;
                var workerDeleteAddr = "/WorkerUser/Delete/" + wu.Id;
                var workerEditAddr = "/WorkerUser/Edit/" + wu.Id;
                var workerPasswordAddr = "/WorkerUser/Password/" + wu.Id;

                var workerDetailLink = $("<a>").html("Więcej").attr("href", workerDetailAddr);
                var workerDeleteLink = $("<a>").html("Usuń").attr("href", workerDeleteAddr);
                var workerEditLink = $("<a>").html("Edytuj").attr("href", workerEditAddr);
                var workerPasswordLink = $("<a>").html("Hasło").attr("href", workerPasswordAddr);

                var workerActions = $("<span>");
                workerActions.append(workerDetailLink).append(" ");
                workerActions.append(workerEditLink).append(" ");
                workerActions.append(workerPasswordLink).append(" ");
                workerActions.append(workerDeleteLink).append(" ");
                
                var isActive = wu.IsActive == true ? "Tak" : "Nie";
                line.append(element.clone().html(wu.Name));
                line.append(element.clone().html(wu.Login));
                line.append(element.clone().html(isActive));
                line.append(element.clone().html(workerActions));
                result.append(line);
            });
            if (hasElements == false) {
                result.append("<tr class=\"gridrow\"><td colspan=\"4\" class=\"center\">Nie znaleziono pracownika</td></tr>");
            }
            resultTable.append(result);
        }

        function GetWorkerSearchResult() {
            var url = "/WorkerUser/JsonWorkerInfos/";
            var json = {
                field: $("select#worker_search_field").val(),
                value: $("input#worker_search_value").val(),
                activity: $("input#worker_activity").is(":checked")
            };
            $.ajax({
                url: url,
                type: "POST",
                success: UpdateWorkerResultTableAnimated,
                data: json
            });
        }

    </script>    
</asp:Content>
